#!/usr/bin/env python

import sys
sys.path.append('../../scripts')
import base
import os
import glob
from xml.sax.saxutils import escape

AVS_OFFICESTUDIO_FILE_DOCUMENT                      = 0x0040
AVS_OFFICESTUDIO_FILE_DOCUMENT_DOCX                 = AVS_OFFICESTUDIO_FILE_DOCUMENT + 0x0001
AVS_OFFICESTUDIO_FILE_DOCUMENT_DOC                  = AVS_OFFICESTUDIO_FILE_DOCUMENT + 0x0002
AVS_OFFICESTUDIO_FILE_DOCUMENT_ODT                  = AVS_OFFICESTUDIO_FILE_DOCUMENT + 0x0003
AVS_OFFICESTUDIO_FILE_DOCUMENT_RTF                  = AVS_OFFICESTUDIO_FILE_DOCUMENT + 0x0004
AVS_OFFICESTUDIO_FILE_DOCUMENT_TXT                  = AVS_OFFICESTUDIO_FILE_DOCUMENT + 0x0005
AVS_OFFICESTUDIO_FILE_DOCUMENT_HTML                 = AVS_OFFICESTUDIO_FILE_DOCUMENT + 0x0006
AVS_OFFICESTUDIO_FILE_DOCUMENT_MHT                  = AVS_OFFICESTUDIO_FILE_DOCUMENT + 0x0007
AVS_OFFICESTUDIO_FILE_DOCUMENT_EPUB                 = AVS_OFFICESTUDIO_FILE_DOCUMENT + 0x0008
AVS_OFFICESTUDIO_FILE_DOCUMENT_FB2                  = AVS_OFFICESTUDIO_FILE_DOCUMENT + 0x0009
AVS_OFFICESTUDIO_FILE_DOCUMENT_MOBI                 = AVS_OFFICESTUDIO_FILE_DOCUMENT + 0x000a
AVS_OFFICESTUDIO_FILE_DOCUMENT_DOCM                 = AVS_OFFICESTUDIO_FILE_DOCUMENT + 0x000b
AVS_OFFICESTUDIO_FILE_DOCUMENT_DOTX                 = AVS_OFFICESTUDIO_FILE_DOCUMENT + 0x000c
AVS_OFFICESTUDIO_FILE_DOCUMENT_DOTM                 = AVS_OFFICESTUDIO_FILE_DOCUMENT + 0x000d
AVS_OFFICESTUDIO_FILE_DOCUMENT_ODT_FLAT             = AVS_OFFICESTUDIO_FILE_DOCUMENT + 0x000e
AVS_OFFICESTUDIO_FILE_DOCUMENT_OTT                  = AVS_OFFICESTUDIO_FILE_DOCUMENT + 0x000f
AVS_OFFICESTUDIO_FILE_DOCUMENT_DOC_FLAT             = AVS_OFFICESTUDIO_FILE_DOCUMENT + 0x0010
AVS_OFFICESTUDIO_FILE_DOCUMENT_DOCX_FLAT            = AVS_OFFICESTUDIO_FILE_DOCUMENT + 0x0011
AVS_OFFICESTUDIO_FILE_DOCUMENT_HTML_IN_CONTAINER    = AVS_OFFICESTUDIO_FILE_DOCUMENT + 0x0012
AVS_OFFICESTUDIO_FILE_DOCUMENT_DOCX_PACKAGE         = AVS_OFFICESTUDIO_FILE_DOCUMENT + 0x0014
AVS_OFFICESTUDIO_FILE_DOCUMENT_OFORM                = AVS_OFFICESTUDIO_FILE_DOCUMENT + 0x0015
AVS_OFFICESTUDIO_FILE_DOCUMENT_DOCXF                = AVS_OFFICESTUDIO_FILE_DOCUMENT + 0x0016

AVS_OFFICESTUDIO_FILE_PRESENTATION                  = 0x0080
AVS_OFFICESTUDIO_FILE_PRESENTATION_PPTX             = AVS_OFFICESTUDIO_FILE_PRESENTATION + 0x0001
AVS_OFFICESTUDIO_FILE_PRESENTATION_PPT              = AVS_OFFICESTUDIO_FILE_PRESENTATION + 0x0002
AVS_OFFICESTUDIO_FILE_PRESENTATION_ODP              = AVS_OFFICESTUDIO_FILE_PRESENTATION + 0x0003
AVS_OFFICESTUDIO_FILE_PRESENTATION_PPSX             = AVS_OFFICESTUDIO_FILE_PRESENTATION + 0x0004
AVS_OFFICESTUDIO_FILE_PRESENTATION_PPTM             = AVS_OFFICESTUDIO_FILE_PRESENTATION + 0x0005
AVS_OFFICESTUDIO_FILE_PRESENTATION_PPSM             = AVS_OFFICESTUDIO_FILE_PRESENTATION + 0x0006
AVS_OFFICESTUDIO_FILE_PRESENTATION_POTX             = AVS_OFFICESTUDIO_FILE_PRESENTATION + 0x0007
AVS_OFFICESTUDIO_FILE_PRESENTATION_POTM             = AVS_OFFICESTUDIO_FILE_PRESENTATION + 0x0008
AVS_OFFICESTUDIO_FILE_PRESENTATION_ODP_FLAT         = AVS_OFFICESTUDIO_FILE_PRESENTATION + 0x0009
AVS_OFFICESTUDIO_FILE_PRESENTATION_OTP              = AVS_OFFICESTUDIO_FILE_PRESENTATION + 0x000a
AVS_OFFICESTUDIO_FILE_PRESENTATION_PPTX_PACKAGE     = AVS_OFFICESTUDIO_FILE_PRESENTATION + 0x000b

AVS_OFFICESTUDIO_FILE_SPREADSHEET                   = 0x0100
AVS_OFFICESTUDIO_FILE_SPREADSHEET_XLSX              = AVS_OFFICESTUDIO_FILE_SPREADSHEET + 0x0001
AVS_OFFICESTUDIO_FILE_SPREADSHEET_XLS               = AVS_OFFICESTUDIO_FILE_SPREADSHEET + 0x0002
AVS_OFFICESTUDIO_FILE_SPREADSHEET_ODS               = AVS_OFFICESTUDIO_FILE_SPREADSHEET + 0x0003
AVS_OFFICESTUDIO_FILE_SPREADSHEET_CSV               = AVS_OFFICESTUDIO_FILE_SPREADSHEET + 0x0004
AVS_OFFICESTUDIO_FILE_SPREADSHEET_XLSM              = AVS_OFFICESTUDIO_FILE_SPREADSHEET + 0x0005
AVS_OFFICESTUDIO_FILE_SPREADSHEET_XLTX              = AVS_OFFICESTUDIO_FILE_SPREADSHEET + 0x0006
AVS_OFFICESTUDIO_FILE_SPREADSHEET_XLTM              = AVS_OFFICESTUDIO_FILE_SPREADSHEET + 0x0007

AVS_OFFICESTUDIO_FILE_SPREADSHEET_XLSB              = AVS_OFFICESTUDIO_FILE_SPREADSHEET + 0x0008
AVS_OFFICESTUDIO_FILE_SPREADSHEET_ODS_FLAT          = AVS_OFFICESTUDIO_FILE_SPREADSHEET + 0x0009
AVS_OFFICESTUDIO_FILE_SPREADSHEET_OTS               = AVS_OFFICESTUDIO_FILE_SPREADSHEET + 0x000a
AVS_OFFICESTUDIO_FILE_SPREADSHEET_XLSX_FLAT         = AVS_OFFICESTUDIO_FILE_SPREADSHEET + 0x000b
AVS_OFFICESTUDIO_FILE_SPREADSHEET_XLSX_PACKAGE      = AVS_OFFICESTUDIO_FILE_SPREADSHEET + 0x000c

AVS_OFFICESTUDIO_FILE_CROSSPLATFORM                 = 0x0200
AVS_OFFICESTUDIO_FILE_CROSSPLATFORM_PDF             = AVS_OFFICESTUDIO_FILE_CROSSPLATFORM + 0x0001
AVS_OFFICESTUDIO_FILE_CROSSPLATFORM_SWF             = AVS_OFFICESTUDIO_FILE_CROSSPLATFORM + 0x0002
AVS_OFFICESTUDIO_FILE_CROSSPLATFORM_DJVU            = AVS_OFFICESTUDIO_FILE_CROSSPLATFORM + 0x0003
AVS_OFFICESTUDIO_FILE_CROSSPLATFORM_XPS             = AVS_OFFICESTUDIO_FILE_CROSSPLATFORM + 0x0004
AVS_OFFICESTUDIO_FILE_CROSSPLATFORM_SVG             = AVS_OFFICESTUDIO_FILE_CROSSPLATFORM + 0x0005
AVS_OFFICESTUDIO_FILE_CROSSPLATFORM_HTMLR           = AVS_OFFICESTUDIO_FILE_CROSSPLATFORM + 0x0006
AVS_OFFICESTUDIO_FILE_CROSSPLATFORM_HTMLRMenu       = AVS_OFFICESTUDIO_FILE_CROSSPLATFORM + 0x0007
AVS_OFFICESTUDIO_FILE_CROSSPLATFORM_HTMLRCanvas     = AVS_OFFICESTUDIO_FILE_CROSSPLATFORM + 0x0008
AVS_OFFICESTUDIO_FILE_CROSSPLATFORM_PDFA            = AVS_OFFICESTUDIO_FILE_CROSSPLATFORM + 0x0009

AVS_OFFICESTUDIO_FILE_IMAGE                         = 0x0400
AVS_OFFICESTUDIO_FILE_IMAGE_JPG                     = AVS_OFFICESTUDIO_FILE_IMAGE + 0x0001
AVS_OFFICESTUDIO_FILE_IMAGE_TIFF                    = AVS_OFFICESTUDIO_FILE_IMAGE + 0x0002
AVS_OFFICESTUDIO_FILE_IMAGE_TGA                     = AVS_OFFICESTUDIO_FILE_IMAGE + 0x0003
AVS_OFFICESTUDIO_FILE_IMAGE_GIF                     = AVS_OFFICESTUDIO_FILE_IMAGE + 0x0004
AVS_OFFICESTUDIO_FILE_IMAGE_PNG                     = AVS_OFFICESTUDIO_FILE_IMAGE + 0x0005
AVS_OFFICESTUDIO_FILE_IMAGE_EMF                     = AVS_OFFICESTUDIO_FILE_IMAGE + 0x0006
AVS_OFFICESTUDIO_FILE_IMAGE_WMF                     = AVS_OFFICESTUDIO_FILE_IMAGE + 0x0007
AVS_OFFICESTUDIO_FILE_IMAGE_BMP                     = AVS_OFFICESTUDIO_FILE_IMAGE + 0x0008
AVS_OFFICESTUDIO_FILE_IMAGE_CR2                     = AVS_OFFICESTUDIO_FILE_IMAGE + 0x0009
AVS_OFFICESTUDIO_FILE_IMAGE_PCX                     = AVS_OFFICESTUDIO_FILE_IMAGE + 0x000a
AVS_OFFICESTUDIO_FILE_IMAGE_RAS                     = AVS_OFFICESTUDIO_FILE_IMAGE + 0x000b
AVS_OFFICESTUDIO_FILE_IMAGE_PSD                     = AVS_OFFICESTUDIO_FILE_IMAGE + 0x000c
AVS_OFFICESTUDIO_FILE_IMAGE_ICO                     = AVS_OFFICESTUDIO_FILE_IMAGE + 0x000d

EXT_TO_FORMAT = {
  "docx" : AVS_OFFICESTUDIO_FILE_DOCUMENT_DOCX,
  "docxf" : AVS_OFFICESTUDIO_FILE_DOCUMENT_DOCXF,
  "oform" : AVS_OFFICESTUDIO_FILE_DOCUMENT_OFORM,
  "dotx" : AVS_OFFICESTUDIO_FILE_DOCUMENT_DOTX,
  "odt" : AVS_OFFICESTUDIO_FILE_DOCUMENT_ODT,
  "ott" : AVS_OFFICESTUDIO_FILE_DOCUMENT_OTT,
  "rtf" : AVS_OFFICESTUDIO_FILE_DOCUMENT_RTF,
  "txt" : AVS_OFFICESTUDIO_FILE_DOCUMENT_TXT,
  "html" : AVS_OFFICESTUDIO_FILE_DOCUMENT_HTML,
  "xlsx" : AVS_OFFICESTUDIO_FILE_SPREADSHEET_XLSX,
  "xltx" : AVS_OFFICESTUDIO_FILE_SPREADSHEET_XLTX,
  "ods" : AVS_OFFICESTUDIO_FILE_SPREADSHEET_ODS,
  "ots" : AVS_OFFICESTUDIO_FILE_SPREADSHEET_OTS,
  "csv" : AVS_OFFICESTUDIO_FILE_SPREADSHEET_CSV,
  "pptx" : AVS_OFFICESTUDIO_FILE_PRESENTATION_PPTX,
  "potx" : AVS_OFFICESTUDIO_FILE_PRESENTATION_POTX,
  "odp" : AVS_OFFICESTUDIO_FILE_PRESENTATION_ODP,
  "otp" : AVS_OFFICESTUDIO_FILE_PRESENTATION_OTP,
  "pdf" : AVS_OFFICESTUDIO_FILE_CROSSPLATFORM_PDF,
  "fb2" : AVS_OFFICESTUDIO_FILE_DOCUMENT_FB2,
  "epub" : AVS_OFFICESTUDIO_FILE_DOCUMENT_EPUB,
  "png" : AVS_OFFICESTUDIO_FILE_IMAGE_PNG,
  "jpg" : AVS_OFFICESTUDIO_FILE_IMAGE_JPG
}

def getFormatByExt(ext):
  format = 0
  try:
    format = EXT_TO_FORMAT[ext]
  except KeyError as e:
    raise ValueError('Undefined format: {}'.format(e.args[0]))
  return format

def getFormatByFile(file_path):
  ext = file_path.split(".")[-1]
  return getFormatByExt(ext)

def convertFile(directory_x2t, file_input, file_output, convert_params):
  cur_path = os.getcwd()

  # fonts directory -----------------------------------
  directory_fonts = directory_x2t + "/sdkjs/common"
  directory_fonts_local = ""
  if "windows" == base.host_platform():
    directory_fonts_local = os.getenv("LOCALAPPDATA") + "/ONLYOFFICE/docbuilder"
  else:
    directory_fonts_local = os.path.expanduser('~') + "/.local/share/ONLYOFFICE/docbuilder"

  if not base.is_file(directory_fonts + "/AllFonts.js") and not base.is_file(directory_fonts_local + "/AllFonts.js"):
    base.cmd_in_dir(directory_x2t, "docbuilder", [], True)
    
  if base.is_file(directory_fonts_local + "/AllFonts.js"):
    directory_fonts = directory_fonts_local
  # ---------------------------------------------------  

  temp_dir = os.getcwd().replace("\\", "/") + "/temp"
  if base.is_dir(temp_dir):
    base.delete_dir(temp_dir)
  base.create_dir(temp_dir)

  xml_convert = u"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  xml_convert += u"<TaskQueueDataConvert>"
  xml_convert += (u"<m_sFileFrom>" + escape(file_input) + u"</m_sFileFrom>")
  xml_convert += (u"<m_sFileTo>" + escape(file_output) + u"</m_sFileTo>")
  xml_convert += u"<m_nFormatTo>" + str(getFormatByFile(file_output)) + u"</m_nFormatTo>"
  xml_convert += (u"<m_sAllFontsPath>" + directory_fonts + u"/AllFonts.js</m_sAllFontsPath>")
  xml_convert += (u"<m_sFontDir>" + directory_fonts + "</m_sFontDir>")
  xml_convert += u"<m_sJsonParams>{&quot;spreadsheetLayout&quot;:{&quot;fitToWidth&quot;:1,&quot;fitToHeight&quot;:1}}</m_sJsonParams>"
  xml_convert += u"<m_nDoctParams>1</m_nDoctParams>"
  xml_convert += convert_params
  xml_convert += (u"<m_sTempDir>" + temp_dir + u"</m_sTempDir>")
  xml_convert += u"</TaskQueueDataConvert>"
  base.save_as_script(temp_dir + "/to.xml", [xml_convert])
  base.cmd_in_dir(directory_x2t, "x2t", [temp_dir + "/to.xml"], True)
  base.delete_dir(temp_dir)

  os.chdir(cur_path)
